package nero.yncc.com.createart.activity;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;

import java.util.ArrayList;
import java.util.HashMap;

import nero.yncc.com.createart.R;
import nero.yncc.com.createart.adapter.AdapterTypeAll;
import nero.yncc.com.createart.base.BaseActivity;
import nero.yncc.com.createart.base.Constants;
import nero.yncc.com.createart.bean.BeanCategory;
import nero.yncc.com.createart.bean.BeanTypeIn;
import nero.yncc.com.createart.custom.MyRecyclerView;
import nero.yncc.com.createart.utils.HttpUtils;

/**
 * Created by nero on 17-6-27.
 * 这个是冒充pop弹出来的所有分类窗体
 */

@ContentView(R.layout.activity_search_all_type)
public class ActivitySearchAllType extends BaseActivity {
    @ViewInject(R.id.rc_type)
    public MyRecyclerView rc_type;
    @ViewInject(R.id.ll_pop_root)
    LinearLayout ll_pop_root;

    AdapterTypeAll adapterTypeAll;

    @ViewInject(R.id.tv_all_type_choose)
    TextView tv_all_type_choose;
    @ViewInject(R.id.ll_allType_choose)
    LinearLayout ll_allType_choose;

    @Override
    protected void initView() {
        super.initView();
    }

    @Override
    protected void initData() {
        super.initData();

        for (BeanCategory beanCategory : ActivitySearchAllGod.arrayList_type) {
            beanCategory.setArrayList(new ArrayList<BeanCategory>());
        }

        adapterTypeAll = new AdapterTypeAll(mContext, this, ActivitySearchAllGod.arrayList_type);
        rc_type.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false));
        rc_type.setAdapter(adapterTypeAll);
        adapterTypeAll.notifyDataSetChanged();
    }

    @Override
    protected void initListener() {
        super.initListener();
        ll_allType_choose.setOnClickListener(this);
        ll_pop_root.setOnClickListener(this);
    }

    @Override
    protected void doClick(int id) {
        super.doClick(id);
        switch (id) {
            case R.id.ll_allType_choose:
                //选中所有
                adapterTypeAll.reset();

                Intent intent = new Intent();
                intent.putExtra("data", new BeanTypeIn("全部", true));
                setResult(200, intent);
                finish();
                break;
            case R.id.ll_pop_root:
                finish();
                break;
        }
    }

    public void noChoose() {
        tv_all_type_choose.setVisibility(View.GONE);
    }

    public void getCategory(String categoryId, final ArrayList<BeanCategory> arrayList) {
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("category_id", categoryId);
        HttpUtils.get(this, Constants.API_CATEGORY, hashMap, null, HttpUtils.TYPE_NOTHING, new HttpUtils.HttpCallback() {
            @Override
            public void success(String msg, Object json) {
                try {
                    JSONArray jsonArray = new JSONObject(json.toString()).getJSONArray("list");
                    for (int a = 0; a < jsonArray.length(); a++) {
                        BeanCategory beanCategory = JSON.parseObject(jsonArray.getString(a), BeanCategory.class);
                        arrayList.add(beanCategory);
                    }
                    adapterTypeAll.notifyDataSetChanged();
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void fail(String failMsg) {

            }
        });
    }

}
